package edu.ustb.efws.schedule;

import edu.ustb.efws.EfwsProperties;
import edu.ustb.efws.pipeline.collect.CollectLauncher;
import edu.ustb.efws.pipeline.collect.SmsLauncher;
import edu.ustb.efws.pipeline.service.*;
import edu.ustb.efws.meter.service.MeterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 定时创建日志表
 *
 * @author TS Group
 */
@Component
public class CreateLogTableSchedule {
    private static final Logger LOGGER = LoggerFactory.getLogger(CreateLogTableSchedule.class);

    private final EfwsProperties properties;
    private final IptLogService iptLogService;
    private final RiskLogService riskLogService;
    private final ThosLogService thosLogService;
    private final KyjLogService kyjLogService;
    private final MeterService meterService;
    private final CollectLauncher collectLauncher;
    private final SmsLauncher smsLauncher;
    private final TableOptService tableOptService;

    @Autowired
    public CreateLogTableSchedule(EfwsProperties properties, IptLogService iptLogService,
                                  RiskLogService riskLogService, ThosLogService thosLogService,
                                  KyjLogService kyjLogService, MeterService meterService,
                                  CollectLauncher collectLauncher, SmsLauncher smsLauncher,
                                  TableOptService tableOptService) {

        this.properties = properties;
        this.iptLogService = iptLogService;
        this.riskLogService = riskLogService;
        this.thosLogService = thosLogService;
        this.kyjLogService = kyjLogService;
        this.meterService = meterService;
        this.collectLauncher = collectLauncher;
        this.smsLauncher = smsLauncher;
        this.tableOptService = tableOptService;
    }

    @PostConstruct
    private void init(){

        if(properties.isCreateTable() || !tableOptService.hasTable()){
            LOGGER.info("Create log table .....");
            iptLogService.createTable();
            riskLogService.createTable();
            thosLogService.createTable();
            kyjLogService.createTable();
            meterService.createTable();
        }

        collectLauncher.start();
        smsLauncher.start();
    }

    @Scheduled(cron = "0 1 0 * * MON")
    public void createPartition(){
        iptLogService.createPartition();
        riskLogService.createPartition();
        thosLogService.createPartition();
        kyjLogService.createPartition();
        meterService.createPartition();
        LOGGER.info("Create partition ....");
    }
}
